﻿/// Author:					Walter Ferrari
/// Created:				2010-01-18
/// ref:                    http://www.snello.it
/// The use and distribution terms for this software are covered by the 
/// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)  
/// By using this software in any fashion, you are agreeing to be bound by 
/// the terms of this license.
///
/// You must not remove this notice, or any other, from this software.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace DataFilesViewer.Controls
{
    [TemplatePart(Name = dfvFilesList.ElementMainTreeViewName, Type = typeof(TreeView))]
    public class dfvFilesList : Control,IdfvControl
    {
        internal TreeView _elementMainTreeView;
        internal const string ElementMainTreeViewName = "MainTreeViewElement";

        public event EventHandler TemplateApplied; 

        /// <summary>
        /// Occurs when Drop.
        /// </summary>
        public event RoutedEventHandler DoDrop;


        /// <summary>
        /// Occurs when changed item selected.
        /// </summary>
        public event RoutedEventHandler SelectionChanged;


        public dfvFilesList()
        {
            this.DefaultStyleKey = typeof(dfvFilesList);
        }

        /// <summary>
        /// Gets or sets the treeview element.
        /// </summary>
        /// <value>The treeview element.</value>
        public TreeView MainTreeViewElement
        {
            get { return _elementMainTreeView; }
            set { _elementMainTreeView = value; }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            object mainTreeViewObj = GetTemplateChild(ElementMainTreeViewName);
            Debug.Assert(typeof(TreeView).IsInstanceOfType(mainTreeViewObj) || (mainTreeViewObj == null),
                "The template part ElementMainTreeViewName is not an instance of TreeView!");
            MainTreeViewElement = mainTreeViewObj as TreeView;

            MainTreeViewElement.SelectedItemChanged += OnMainTreeViewElementSelectionChanged;
            MainTreeViewElement.Drop += MainTreeView_doDrop;
            MainTreeViewElement.DragEnter += MainTreeView_doDragEnter;
            MainTreeViewElement.DragLeave += MainTreeView_doDragLeave;
        }

        #region [ MainTreeViewElement HANDLERS ]
        private void OnMainTreeViewElementSelectionChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView view = sender as TreeView;
            TreeViewItem tvi = view.SelectedItem as TreeViewItem;
            string selectedItem = tvi.Header.ToString();

            TreeViewItem tvParent = tvi.Parent as TreeViewItem;
            if (tvParent != null)
                selectedItem = tvParent.Header.ToString();

            SelectionChanged(selectedItem, e);

/*
            foreach (KeyValuePair<string, IFileDropFormat> keyValuePair in droppedFiles)
            {
                if (rootNode == keyValuePair.Key)
                {
                    IFileDropFormat filedroppedObj = keyValuePair.Value;

                    UpdateTabControl(filedroppedObj, selectedItem);

                    /*  ORIGINAL WITH Datagrid at the bottom
                    foreach (string subitem in filedroppedObj.GetListSubItems())
                    {
                        if (subitem == selectedItem)  // NB TO DO: perorm comparison also with root tree node name
                        {
                            UpdateMainGrid(filedroppedObj, selectedItem);
                            //ShowChart(datasource);
                            return;
                        }
                    }
                     // endof original
                }
            }
    */
        }

        private void MainTreeView_doDrop(object sender, DragEventArgs e)
        {
            if (DoDrop != null)
            {
                DoDrop(sender, e);
            }
            //ManageDrop(sender, e);
        }

        private void MainTreeView_doDragEnter(object sender, DragEventArgs e)
        {
        }

        private void MainTreeView_doDragLeave(object sender, DragEventArgs e)
        {
            e.Handled = true;
        }


        public void AddItems(List<string> listNames, string rootName)
        {
            if (listNames == null)
                return;

            foreach (TreeViewItem item in MainTreeViewElement.Items)
            {
                if (item.Header.ToString() == rootName)
                    return;
            }

            TreeViewItem itemTmp = new TreeViewItem();
            itemTmp.Header = rootName;

            foreach (string sItem in listNames)
            {
                itemTmp.Items.Add(new TreeViewItem() { Header = sItem });
            }
            MainTreeViewElement.Items.Add(itemTmp);

        }



        #endregion
    }
}
